package com.alphas.xixibackendalphas.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**
 * 自定义Swagger接口文档定义
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
//    public Docket docket(){
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .select()
//                //这里一定要标注你控制器的位置
//                .apis(RequestHandlerSelectors.basePackage("com.alphas.xixibackendalphas.controller"))
//                .paths(PathSelectors.any())
//                .build();
//    }
    public Docket initDocket(Environment env) {
        //设置要暴漏接口文档的配置环境
        //设置要显示的Swagger环境
        Profiles profile = Profiles.of("test","dev");
        //获取项目的环境：
        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = env.acceptsProfiles(profile);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                //是否启动swagger 默认为true ,如果为false，则Swagger不能再浏览器中访问
                .enable(flag)
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
//                .apis(RequestHandlerSelectors.any()):扫描全部
//                .apis(RequestHandlerSelectors.none()):不扫描
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)):扫描类上的注解，参数是一个注解的反射对象
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class)):扫描方法上的注解
                //指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.alphas.xixibackendalphas.controller"))
                //paths()过滤什么路径(url)
//                .paths(PathSelectors.ant("/pm/**")) 就是在localhost:8080/pm后面的路径
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //不过滤
                .paths(PathSelectors.any())
                .build();
        //右上角 组（有几个Docket，有几个组）
//                .groupName();
    }
    // 配置swagger基本信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("Alphas", "https://gitee.com/alphaswilson", "574509572@qq.com");
        return new ApiInfo(
                "汐昔伙伴匹配系统",
                "汐昔伙伴匹配系统接口文档",
                "1.0",
                "https://gitee.com/alphaswilson",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
